import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/HomeIndex'
import Login from '@/views/LoginIndex'
import Echarts from '@/views/SecondLevel/EchartsHome'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Home,
    redirect: '/echarts',
    children: [
      {
        path: '/echarts',
        component: Echarts
      }
    ]
  },
  {
    path: '/login',
    component: Login

  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(async (to, from, next) => {
  const { token } = await store.state.user
  if (to.path !== '/login' && !token) return next('/login')
  next()
})

export default router
